package com.t235.gc.goods.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.t235.gc.AjaxResult;
import com.t235.gc.entity.DeviceCabinet;
import com.t235.gc.entity.Goods;
import com.t235.gc.entity.GoodsCategory;
import com.t235.gc.goods.service.GoodsService;
import com.t235.gc.vo.GoodsVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品表(Goods)表控制层
 *
 * @author makejava
 * @since 2021-03-24 08:45:31
 */
@RestController
@RequestMapping("/api")
@Api
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 设备商品列表
     * @param goodsVo
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/goods/admin/getBackGoodsList")
    public AjaxResult getBackDeviceCabinetList(@RequestBody GoodsVo goodsVo, Integer page , Integer limit) {
        PageHelper.startPage(page, limit);
        List<DeviceCabinet> list = goodsService.selectByIsFaultAndIsStock(goodsVo);
        PageInfo<DeviceCabinet> pageInfo = new PageInfo<>(list);
        return AjaxResult.success(pageInfo);
    }

    /**
     *补货
     * @param cabinetid
     * @param stockqty
     * @return
     */
    @PostMapping("/device-cabinet/admin/setIsStock")
    public AjaxResult setIsStock(Long cabinetid, Integer stockqty){
        if (goodsService.updateStockQty(cabinetid,stockqty)){
            return AjaxResult.success("补货成功");
        }
        return AjaxResult.error("补货失败");
    }

    /**
     *上下架商品
     * @param isflag
     * @param cabinetid
     * @return
     */
    @GetMapping("/goods/admin/updateIsOff")
    public AjaxResult updateIsOff(Integer isflag, Long cabinetid) {
        if (goodsService.updateSxj(isflag,cabinetid)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 所有商品分类
     * @return
     */
    @GetMapping("/goods-category/admin/getAllGoodsCategoryList")
    public Object getBackGoodsCategoryList(){
        List<GoodsCategory> list=goodsService.selectCategory();
        return list;
    }

    /**
     * 商品列表
     * @param goodscategoryid
     * @return
     */
    @GetMapping("/goods/admin/getGoodsList")
    public Object getBackGoodsList(Long goodscategoryid){
        List<Goods> list=goodsService.selectGoodsNameById(goodscategoryid);
        return list;
    }

    /**
     * 商品积分
     * @param goodsid
     * @return
     */
    @GetMapping("/goods/admin/getAccpointsImg")
    public Object goodsAccpointsImg(Long goodsid){
        Goods goods=goodsService.selectByPrimaryKey(goodsid);
        return goods;
    }

    /**
     * 新增或修改商品
     * @param goodsid
     * @param cabinetid
     * @return
     */
    @PostMapping("/goods/admin/saveOrUpdateGoods")
    public AjaxResult add(Long goodsid, Long cabinetid){
        if (goodsService.updateDeviceCabinetBygoodscategoryid(goodsid,cabinetid)){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
